﻿using System;
using System.Windows.Forms;

namespace CalcSharp.MDIUI
{
    public partial class windowlist : Form
    {
        public windowlist()
        {
            InitializeComponent();
        }

        private void redrawList()
        {
            WinList.Items.Clear();
            ListViewItem tmp;
            ImageList icons = new ImageList();
            int c = 0;
            WinList.SmallImageList = icons;
            WinList.LargeImageList = icons;
            foreach (Form f in this.MdiParent.MdiChildren)
            {
                tmp = new ListViewItem();
                tmp.Name = f.Name;
                tmp.Text = f.Text;
                icons.Images.Add(f.Icon);
                tmp.ImageIndex = c;
                tmp.Group = WinList.Groups[0];
                ++c;
                WinList.Items.Add(tmp);
            }
        }

        private void windowlist_Shown(object sender, EventArgs e)
        {
            redrawList();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_tofront_Click(object sender, EventArgs e)
        {
            if (WinList.SelectedItems.Count > 0)
            {
                foreach (Form f in this.MdiParent.MdiChildren)
                {
                    if (WinList.SelectedItems[0].Name == f.Name)
                    {
                        if (f.WindowState == FormWindowState.Minimized) f.WindowState = FormWindowState.Normal;
                        f.Show();
                        f.BringToFront();
                    }
                }
            }
        }

        private void WinList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (WinList.SelectedIndices.Count > 0)
            {
                frm_tofront.Visible = true;
                frm_close.Visible = true;
            }
            else
            {
                frm_close.Visible = false;
                frm_tofront.Visible = false;
            }
        }

        private void frm_close_Click(object sender, EventArgs e)
        {
            if (WinList.SelectedItems.Count > 0)
            {
                foreach (Form f in this.MdiParent.MdiChildren)
                {
                    if (WinList.SelectedItems[0].Name == f.Name)
                    {
                        f.Close();
                    }
                }
            }
            if (WinList.SelectedItems[0].Name != this.Name) redrawList();
        }
    }
}